<template>
  <div class="login-container">
    <a-card class="login-card" title="系统登录" :bordered="false">
      <a-row :gutter="[16, 16]">
        <a-col :span="24">
          <a-form
            :model="formState"
            name="basic"
            :label-col="{ span: 0 }"
            :wrapper-col="{ span: 24 }"
            autocomplete="off"
            @finish="onFinish"
            style="width: 100%"
          >
        <a-form-item
          name="username"
          :rules="[{ required: true, message: '请输入用户名!' }]"
        >
          <a-input v-model:value="formState.username" placeholder="用户名" >
            <template #prefix>
              <UserOutlined style="color: rgba(0, 0, 0, 0.25)" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
          name="password"
          :rules="[{ required: true, message: '请输入密码!' }]"
        >
          <a-input-password v-model:value="formState.password" placeholder="密码" >
            <template #prefix>
              <LockOutlined style="color: rgba(0, 0, 0, 0.25)" />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
              <a-button type="primary" html-type="submit" :loading="loading">登录</a-button>
            </a-form-item>
          </a-form>
        </a-col>
      </a-row>
    </a-card>
  </div>
</template>

<script>
import { defineComponent, reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
import { message } from 'ant-design-vue';
import axios from 'axios';
import { encryptPassword } from '@/utils/crypto';
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';

export default defineComponent({
  name: 'LoginView',
  components: {
    UserOutlined,
    LockOutlined
  },
  setup() {
    const router = useRouter();
    const store = useStore();
    const loading = ref(false);

    const formState = reactive({
      username: '',
      password: ''
    });

    const onFinish = async (values) => {
      loading.value = true;
      try {
        // 调用登录接口
        const { data } = await axios.post('/api/admin/login', {
          username: values.username,
          password: encryptPassword(values.password)
          // password: values.password
        });
        
        if (data.code === 200) {
          // 获取用户权限
          let permissions = [];
          try {
            const permissionResponse = await axios.get('/api/admin/permissions');
            if (permissionResponse.data.code === 200) {
              permissions = permissionResponse.data.data || [];
            }
          } catch (error) {
            console.error('获取用户权限失败:', error);
          }

          // 存储登录信息
          store.dispatch({
            type: 'login',
            data: {
              userName: data.data.username,
              userNick: data.data.nickname,
              userAccount: data.data.username,
              role: data.data.roles || [],
              avatar: data.data.avatar,
              permissions: permissions
            }
          });
          
          // 存储token
          localStorage.setItem('token', data.data.token);
          
          message.success('登录成功');
          router.push('/');
        } else {
          message.error(data.message || '登录失败');
        }
      } catch (error) {
        console.error('登录错误:', error);
        message.error('登录失败，请稍后重试');
      } finally {
        loading.value = false;
      }
    };

    return {
      formState,
      loading,
      onFinish
    };
  }
});
</script>

<style scoped>
.login-container {
  background: url('/public/background.png') no-repeat center center;
  background-size: cover;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.login-card {
  width: 420px;
  border-radius: 12px;
  box-shadow: 0 6px 32px rgba(0, 0, 0, 0.16);
  background-color: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(10px);
  padding: 20px 32px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-card .ant-card-head {
  border-bottom: none;
}

.login-card .ant-card-head-title {
  text-align: center;
  font-size: 20px;
  font-weight: 500;
}

.login-card .ant-form-item-label {
  font-weight: 500;
}

.login-card .ant-btn {
  width: 100%;
  margin-top: 16px;
}
</style>